package com.islewars.client;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.CanvasGradient;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.canvas.dom.client.CssColor;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.islewars.client.dto.MountainDto;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class IsleWars implements EntryPoint {
    Canvas canvas;
    Context2d context;
    static final int canvasWidth = 1000;
    static final int canvasHeight = 768;
    List<MountainDto> mountains;
    Vector start;
    Vector end;
    
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		LoginForm loginForm = new LoginForm();	
		RootPanel.get().add(loginForm.getPanel());

//        canvas = Canvas.createIfSupported();
//        
//        if (canvas == null) {
//              RootPanel.get().add(new Label("Sorry, your browser doesn't support the HTML5 Canvas element"));
//              return;
//        }
// 
//        canvas.setStyleName("mainCanvas");
//        canvas.setWidth(canvasWidth + "px");
//        canvas.setCoordinateSpaceWidth(canvasWidth);
// 
//        canvas.setHeight(canvasHeight + "px");
//        canvas.setCoordinateSpaceHeight(canvasHeight);
// 
//        RootPanel.get().add(canvas);
//        context = canvas.getContext2d();
//        
//        setup();
//        
//        draw();
	}
	
	private void setup() {
		mountains = new ArrayList<MountainDto>();

		start = new Vector(10, 310);
		end = new Vector(1000, 310);
		
		MountainDto mountain = new MountainDto();
		mountain.x=300.0;
		mountain.y=300.0;
		mountain.type = MountainType.MOUNTAIN;
		mountain.radius = 400.0;
		mountain.height = 400;
		mountains.add(mountain);
		
		MountainDto mountain2 = new MountainDto();
		mountain2.x=1000.0;
		mountain2.y=300.0;
		mountain2.type = MountainType.CONE;
		mountain2.radius = 200.0;
		mountain2.height = 400.0;
		mountains.add(mountain2);

		MountainDto mountain3 = new MountainDto();
		mountain3.x=500.0;
		mountain3.y=300.0;
		mountain3.type = MountainType.HILL;
		mountain3.radius = 300.0;
		mountain3.height = 100.0;
		mountains.add(mountain3);
		
		MountainDto mountain4 = new MountainDto();
		mountain4.x=700.0;
		mountain4.y=300.0;
		mountain4.type = MountainType.MOUNTAIN;
		mountain4.radius = 300.0;
		mountain4.height = 200;
		mountains.add(mountain4);
	}

	private void draw() {
		Vector position = new Vector(start);

		Vector speed = Vector.mult(Vector.sub(end, start), 1.0/100.0);
		
		CanvasGradient grd=context.createLinearGradient(512, 0, 512, 768);
		grd.addColorStop(0,"#8ED6FF"); // light blue
		grd.addColorStop(1,"#004CB3"); // dark blue
		context.setFillStyle(grd);
	    context.beginPath();
	    context.fillRect(0, 0, 1000, 768);
	    context.closePath();
	    context.fill();

		context.beginPath();
		context.moveTo(position.x, position.y);

		double maxHeight = Double.MIN_VALUE;
		double minHeight = Double.MAX_VALUE;
		
		for (int i=1; i<=100; i++) {
			double calculatedHeight = Calculation.getHeight(position, mountains);	

			if (calculatedHeight>maxHeight) {
				maxHeight = calculatedHeight;
			}
			if (calculatedHeight<minHeight) {
				minHeight = calculatedHeight;
			}

//			System.out.println("-----------------");
//			System.out.println("x: " + position.x);
//			System.out.println("y: " + position.y);
//			System.out.println("height: " + calculatedHeight);

			position.add(speed);
		}
		
		position = new Vector(start);

		double heightDifference = maxHeight - minHeight;
		System.out.println("maxHeight: " + maxHeight + ", minHeight: " + minHeight + ", difference: " + heightDifference);
		System.out.println("-----------------");

		for (int i=1; i<=100; i++) {

			double height = Calculation.getHeight(position, mountains);	// TODO use already calculated vales

			double calculatedHeight = heightDifference!=0.0 ? (height - minHeight)*400/heightDifference : height;
			
			if (i==1) {
				context.moveTo(i*10, 768-calculatedHeight);
			}
			
			context.lineTo(i*10, 768-calculatedHeight);

//			System.out.println("-----------------");
//			System.out.println("x: " + position.x);
//			System.out.println("y: " + position.y);
//			System.out.println("height: " + height);
//			System.out.println("calculatedHeight: " + calculatedHeight);
			
			position.add(speed);
		}

		context.lineTo(100*10, canvasHeight);
		context.lineTo(10, canvasHeight);
		context.closePath();
		
	    context.setLineWidth(5);
	    context.setStrokeStyle("#40aa30");
		context.stroke();

		context.setFillStyle("#994C00");

		context.fill();
		
		context.beginPath();
		context.moveTo(0, 700);

		for (int i=1; i<=100; i++) {
			Random rn = new Random();
			double height = rn.nextDouble()*5.0;
			context.lineTo(i*10, 700+height);
		}

		context.lineTo(1000, 768);
		context.lineTo(0, 768);

		context.setFillStyle(CssColor.make("rgba(130,130,200,0.6)"));
		context.fill();
	}
}
